package com.example.seakdreamandroid.bonus;

import static com.example.seakdreamandroid.Constant.BASE_URL;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.Lullaby.LullabyGridAdapter;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.MemberActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.chatbot.ChatbotActivity;
import com.example.seakdreamandroid.crosstalk.Audio;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.sleep.NapActivity;
import com.example.seakdreamandroid.sleep.StartSleep;
import com.example.seakdreamandroid.whiteNoise.WhiteNoiseActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BonusPointsAdapter extends BaseAdapter {
    private User user;
    private BonusPointsActivity context;
    private LayoutInflater layoutInflater;
    private List<Map<String,Object>> list;

    public BonusPointsAdapter(BonusPointsActivity context, List<Map<String,Object>> list) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        user = (User) context.getApplication();
        View view = null;
        ViewHolder holder = null;
        if (null == convertView) {
            //绑定Item布局

            holder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.activity_bonus_item, null);
            //自定义内部类，对象holder用来存储文字和图片控件

            holder.mTextView = view.findViewById(R.id.item_bonus);
            holder.missionPoint = view.findViewById(R.id.mission_bonus);

            //将holder放入当前视图中
            view.setTag(holder);
        } else {
            view = convertView;
            //复用holder
            holder = (ViewHolder) view.getTag();
        }
        //取出app对象
        holder.mTextView.setText((String)list.get(position).get("Mission"));
        int bonus = (Integer) list.get(position).get("point");
        String xbonus = "+" + bonus + "积分(vip双倍)";
        holder.missionPoint.setText(xbonus);
        Button button = view.findViewById(R.id.task_btn);
        System.out.println(list.get(position));
        int xdone = (Integer) list.get(position).get("done");
        System.out.println(xdone);
        switch (xdone){
            case 1:
                button.setBackgroundResource(R.mipmap.whitenoisebackground);
                button.setText("已完成");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast toast = Toast.makeText(context,"今日已完成该任务",Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
                break;
            case 0:
                button.setText("待完成");
                String text = (String) list.get(position).get("Mission");
                System.out.println(text);
                int i = (int)list.get(position).get("Button");

                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent();
                        int x = user.getPoints();
                        String xvip = user.getIf_vip();
                        System.out.println("xvip=" + xvip);
                        switch (i){
                            case 1:
                                missionFinish(i,button,position);
                                if (xvip.equals("1")){
                                    context.setNewPoints(x + 30);
                                    System.out.println("是vip");
                                }else {
                                    context.setNewPoints(x + 15);
                                }

                                break;
                            case 2:
                                intent.setClass(context, WhiteNoiseActivity.class);
                                context.startActivity(intent);
                                missionFinish(i,button,position);
                                if (xvip.equals("1")){
                                    System.out.println("是vip");
                                    context.setNewPoints(x + 20);
                                }else {
                                    context.setNewPoints(x + 10);
                                }

                                break;
                            case 3:
                                intent.setClass(context, NapActivity.class);
                                missionFinish(i,button,position);
                                context.startActivity(intent);
                                if (xvip.equals("1")){
                                    context.setNewPoints(x + 50);
                                }else {
                                    context.setNewPoints(x + 25);
                                }

                                break;
                            case 4:
                                intent.setClass(context, ChatbotActivity.class);
                                missionFinish(i,button,position);
                                context.startActivity(intent);
                                if (xvip.equals("1")){
                                    context.setNewPoints(x + 20);
                                }else {
                                    context.setNewPoints(x + 10);
                                }
                                break;
                        }
                    }
                });
        }

        String text = (String) list.get(position).get("Mission");
        Log.v("hc", "getView结束：position=" + position +
                ",textContent=" + text.toString() +
                ",convertView=" + convertView
        );//可以通过hc标签查看logcat
        return view;
    }
    //内部类
    final static class ViewHolder {
        TextView mTextView;
        TextView missionPoint;
        Button button;
    }


    public void missionFinish(int key,Button button,int position){
        FormBody body = new FormBody.Builder()
                .build();
        //3.创建请求对象（Request）
        Request request = new Request.Builder()
                //设置
                .url(Constant.BASE_URL + "Bonus/missionUpdate"+"?key="+key)
                //创建Request对象
                .build();
        //4.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Log.e("msg","失败");
                        button.setText("已完成");
                        button.setBackgroundResource(R.mipmap.whitenoisebackground);

                        Toast toast = Toast.makeText(context,"任务已完成",Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list.get(position).put("done",1);
                        context.bonusPointsAdapter.notifyDataSetChanged();
                        Log.e("missionUpdate","成功");
                        button.setText("已完成");
                        button.setBackgroundResource(R.mipmap.whitenoisebackground);
                        Toast toast = Toast.makeText(context,"恭喜完成任务",Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
            }
        });
    }

}
